package com.todod.base.module.base.service.rthistory;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.todod.base.module.base.controller.admin.rthistory.vo.*;
import com.todod.base.module.base.dal.dataobject.rthistory.RtHistoryDO;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.util.object.BeanUtils;

import com.todod.base.module.base.dal.mysql.rthistory.RtHistoryMapper;

import static com.todod.base.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.todod.base.module.base.enums.ErrorCodeConstants.*;

/**
 * 历史数据管理 Service 实现类
 *
 * @author 超管儿
 */
@Slf4j
@Service
@Validated
public class RtHistoryServiceImpl implements RtHistoryService {

    @Resource
    private RtHistoryMapper rtHistoryMapper;

    @Override
    public Long createRtHistory(RtHistorySaveReqVO createReqVO) {
        // 插入
        RtHistoryDO rtHistory = BeanUtils.toBean(createReqVO, RtHistoryDO.class);
        rtHistoryMapper.insert(rtHistory);
        // 返回
        return rtHistory.getId();
    }

    @Override
    public void updateRtHistory(RtHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateRtHistoryExists(updateReqVO.getId());
        // 更新
        RtHistoryDO updateObj = BeanUtils.toBean(updateReqVO, RtHistoryDO.class);
        rtHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteRtHistory(Long id) {
        // 校验存在
        validateRtHistoryExists(id);
        // 删除
        rtHistoryMapper.deleteById(id);

    }

    private void validateRtHistoryExists(Long id) {
        if (rtHistoryMapper.selectById(id) == null) {
            throw exception(RT_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public RtHistoryDO getRtHistory(Long id) {
        return rtHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<RtHistoryDO> getRtHistoryPage(RtHistoryPageReqVO pageReqVO) {
        return rtHistoryMapper.selectPage(pageReqVO);
    }


    /**
     * 一键物理删除历史年度数据
     */
    @Override
    public void cleanAll(){
        log.info("对数据表进行备份");
        backupTable();
        log.warn("物理删除所有数据表数据");
        rtHistoryMapper.cleanAll();
    }


    public void backupTable(){

        String table = "base_rt_history";

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = now.format(formatters);
        String backupTable  ="bak_" + table + "_" +  formattedDateTime;
        rtHistoryMapper.createBackupTable(backupTable, table);
        rtHistoryMapper.insertIntoBackupTable(backupTable, table);
    }


    /**
     * 根据年份物理删除数据表数据
     */
    public void deleteByYear(String year){
        if(year.equals("undefined")||year=="undefined"){
            year = null;
        }
        //验证年份是否为空
        if ( year == null ||year.isEmpty()) {
            throw exception(RT_HISTORY_YEAR_NULL);
        }
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();
        log.info(String.valueOf(currentYear));
        //验证不能删除当年数据
        if(String.valueOf(currentYear).equals(year)||String.valueOf(currentYear) == year){
            throw exception(RT_HISTORY_NOW_YEAR);
        }
        //执行删除操作
        rtHistoryMapper.deleteByYear(year);
    }


}